﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class DigitalTwinRepository : BaseRepository<TInventory>, IDigitalTwinRepository
    {
        public DigitalTwinRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }


        /// <summary>
        /// 根据仓位编码查询物料信息
        /// </summary>
        /// <param name="CellCode"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TInventory> GetMaterialsByCell(string CellCode)
        {
            var MaterialList = Context.TInventories.Where(x => x.LocationCode == CellCode).ToList();
            return MaterialList;
        }

        /// <summary>
        /// 出入库每日统计
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ReceivingShippingEverydayQty GetReceivingShippingEverydayQty()
        {
            var today = DateTime.Today;

            var receivingListQty = Context.TReceivingOrderDetails
                .Where(x => x.CreateDate >= today)
                .GroupBy(x => x.OrderSn);
            var receivingQtyEveryday = receivingListQty.Count();

            var shippingListQty = Context.TShippingOrderDetails
                .Where(x => x.CreateDate >= today)
                .GroupBy(x => x.OrderSn);
            var shippingQtyEveryday = shippingListQty.Count();

            return new ReceivingShippingEverydayQty
            {
                ReceivingQtyEveryday = receivingQtyEveryday,
                ShippingQtyEveryday = shippingQtyEveryday
            };
        }

        /// <summary>
        /// 出入库统计
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ReceivingShippingQty GetReceivingShippingQty()
        {
            var receivingListQty = Context.TReceivingOrderDetails.GroupBy(x => x.OrderSn);
            var receivingQty = receivingListQty.Count();
            var shippingListQty = Context.TShippingOrderDetails.GroupBy(x => x.OrderSn);
            var shippingQty = shippingListQty.Count();
            return new ReceivingShippingQty
            {
                ReceivingQty = receivingQty,
                ShippingQty = shippingQty
            };
        }

        /// <summary>
        /// 移库盘库统计
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public StocktakingTransferBinQty GetStocktakingTransferBinQty()
        {
            var StocktakingListQty = Context.TStocktakingOrderDetails.GroupBy(x => x.OrderSn);
            var stocktakingQty = StocktakingListQty.Count();
            var TransferBinListQty = Context.TTransferBinOrders.GroupBy(x => x.OrderSn);
            var transferBin = TransferBinListQty.Count();
            return new StocktakingTransferBinQty
            {
                StocktakingQty = stocktakingQty,
                TransferBinQty = transferBin
            };
        }

        /// <summary>
        /// 获取器材总数
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public decimal? GetMaterialQty()
        {
            var Qty = Context.TInventories.AsQueryable().Sum(x => x.AvailableQty);
            return Qty;
        }


        public List<ReceivingShippingEveryWorkQty> GetReceivingShippingWeeklyQty()
        {
            var startDate = DateTime.Today.AddDays(-3); // 从一周前开始计算
            var endDate = DateTime.Today; // 当天

            var ReceivingList = Context.TReceivingOrderDetails
                   .Where(x => x.CreateDate >= startDate && x.CreateDate <= endDate)
                   .ToList();

            var groupedReceivingList = ReceivingList
                .GroupBy(x => x.CreateDate.Date)
                .ToList();

            var ShippingList = Context.TShippingOrderDetails
                 .Where(x => x.CreateDate >= startDate && x.CreateDate <= endDate)
                 .ToList();

            var groupedShippingList = ShippingList
                .GroupBy(x => x.CreateDate.Date)
                .ToList();

            List<ReceivingShippingEveryWorkQty> list = new List<ReceivingShippingEveryWorkQty>();
            for (int i = 3; i >= 0; i--)
            {
                var currentDate = endDate.AddDays(-i).Date;
                var ReceivingGroup = groupedReceivingList.FirstOrDefault(x => x.Key == currentDate);

                var ShippingGroup = groupedShippingList.FirstOrDefault(x => x.Key == currentDate);

                int Receivingqty = ReceivingGroup != null ? ReceivingGroup.Count() : 0;
                int Shippingqty = ShippingGroup != null ? ShippingGroup.Count() : 0;


                list.Add(new ReceivingShippingEveryWorkQty
                {
                    WeekNumber = currentDate.ToString().Substring(5, 5),
                    ReceivingQty = Receivingqty,
                    ShippingQty = Shippingqty
                });
            }
            return list;
        }
    }
}
